
local Skill = require("app.public.dao.Skill")

local string_split = string.split
local string_ulen = string.ulen
local string_usub = string.usub
local Utils = {}

function Utils.getSkill(id)
    if not id then
        return
    end
    if id<=0 then
        return
    end
    local s = Skill.new()
    s:read( { uid = id } )

    local team = s:getTeam()
    local skillId, skillPars
    local mana, rage, energy, cd, combopoint = 0, 0, 0, 0, 0

    for k, learnedSkill in pairs( UD:getSkills() ) do
        if learnedSkill:getTeam() == team then
            --是本技能
            --已学习
            s:setLearned()

            skillId = 0
            skillPars = string_split( learnedSkill:getSkillPar() , ";" )
            for _,par in pairs( skillPars ) do
                local skillParArray = string_split(par,"|")
                if skillParArray[1]=="1" then
                    skillId = tonumber(skillParArray[2])
                    break
                end
            end
            mana, rage, energy, cd, combopoint = UD:getSkillCost(skillId, learnedSkill:getSkillLv())
            s:setMana(mana)
            s:setRage(rage)
            s:setEnergy(energy)
            s:setCd(cd)
            s:setComboPoint(combopoint)

            break
        end
    end
    return s
end

---------------------------------------<<<<<<<<<<<<<< 以上：解析技能消耗、CD相关

-- 构造base.proto协议一致的UserBaseInfo数据结构
-- 不用传给服务器的userbase结构用 UserBaseInfo 初始化
function Utils.createUserBaseInfoStruct( info )
    local temp = {}
    temp.uid = info and info.uid or 0
    temp.name = info and info.name or ""
    temp.lv = info and info.lv or 0
    temp.raceID = info and info.raceID or 0
    temp.classID = info and info.classID or 0
    temp.specialistID = info and info.specialistID or 0
    temp.gender = info and info.gender or 0
    temp.faceID = info and info.faceID or 0
    temp.hairID = info and info.hairID or 0
    temp.hairColorID = info and info.hairColorID or 0
    temp.fashionIDs = info and info.fashionIDs or {}
    temp.mountID = info and info.mountID or 0
    temp.titleID = info and info.titleID or 0
    temp.guildName = info and info.guildName or ""
    temp.guildID = info and info.guildID or ""
    temp.combatPower = info and info.combatPower or 0
    temp.serverID = info and info.serverID or 0

    temp.partners = {}
    if info then
        for k , v in pairs(info.partners) do
            table.insert(temp.partners, {ID = v.ID, RaceId = v.RaceId, CombatPower = v.CombatPower, Lv = v.Lv})
        end
    end
    -- temp.PartnerQuality = info and info.PartnerQuality or 0
    temp.mainTaskID = info and info.mainTaskID or 0

    return temp
end



function Utils.getStringInLen(str, maxLen)
    local len = string_ulen(str)
    if len > maxLen then
        return string_usub(str, 1, maxLen-1) .. "..."
    else
        return str
    end
end

return Utils
